package com.yanlight.app.controller;

import com.yanlight.app.dal.entity.PayCode;
import com.yanlight.app.response.CompanyDetailResponse;
import com.yanlight.app.service.CompanyService;
import com.yanlight.app.service.PayCodeService;
import com.yanlight.exception.ErrorType;
import com.yanlight.support.http.Response;
import com.yanlight.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/appManager")
public class AppManagerController {

    @Autowired
    PayCodeService payCodeService;

    @Autowired
    CompanyService companyService;

    @RequestMapping("/payCodeInfo")
    @ResponseBody
    public Response payCodeInfo(String value) {
        PayCode payCode = payCodeService.queryInfo(value);
        if (payCode == null) {
            return ResponseUtil.fail(ErrorType.NOT_EXIST_RECORD);
        } else {
            return ResponseUtil.success(payCode);
        }
    }

    /**
     * 修改聚合码信息
     *
     * @param payCode
     * @return
     */
    @RequestMapping("/updatePayCode")
    @ResponseBody
    public Response updatePayCode(PayCode payCode) {
        PayCode update = payCodeService.update(payCode);
        if (update == null) {
            return ResponseUtil.fail(ErrorType.SYSTEM_ERROR);
        } else {
            return ResponseUtil.success(update);
        }
    }

    @RequestMapping("/companyInfo")
    @ResponseBody
    public Response companyInfo(Integer companyId) {
        CompanyDetailResponse companyDetailResponse = companyService.findById(companyId);
        if (companyDetailResponse == null) {
            return ResponseUtil.fail(ErrorType.NOT_EXIST_RECORD);
        } else {
            return ResponseUtil.success(companyDetailResponse);
        }
    }
}
